/**
 * rmq 链接相关
 */
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import type { IRmqFormState } from "@/typings/rmq";
import { RabbitMq_Config } from "@/../public/config";

interface RmqState {
  rmqIp: string;
  rmqUsername: string;
  rmqPassword: string;
  rmqHost: string;
  gameId: string; // 游戏ID
  isWebRTC: boolean; // 是否可视电话
  connectState: boolean; // rmq 链接状态
}

export const rmqState = defineStore("rmq-store", () => {
  const rmqIp = ref("");
  const rmqUsername = ref("");
  const rmqPassword = ref("");
  const rmqHost = ref("/");
  const gameId = ref("");
  const isWebRTC = ref(false);
  const connectState = ref(false);

  const getRmqUrl = computed(() => {
    const ip = rmqIp.value ? rmqIp.value.trim() : RabbitMq_Config.ip, // 默认IP
      port = 1567;
    return `ws://${ip}:${port}/ws`;
  });

  const observationSubs = computed(() => {
    const observationUri = `/exchange/${gameId.value}-observe/`;
    return {
      all: `${observationUri}all`,
      red: `${observationUri}red-info`,
      blue: `${observationUri}blue-info`,
    };
  });

  const fireAndInfoSubs = computed(() => {
    const observationUri = `/exchange/${gameId.value}-domain/`;
    return {
      red: `${observationUri}red-info`,
      blue: `${observationUri}blue-info`,
    };
  });

  const getObservationSubs = computed(() => observationSubs.value);
  const getFireAndInfoSubs = computed(() => fireAndInfoSubs.value);
  const getRmqHost = computed(() => rmqHost.value);
  const getRmqUsername = computed(() => rmqUsername.value);
  const getRmqPassword = computed(() => rmqPassword.value);
  const getGameId = computed(() => gameId.value);

  function setConnectState(params: boolean) {
    connectState.value = params;
  }

  function setRmqValue(params: IRmqFormState) {
    rmqIp.value = params.rmqIp;
    rmqUsername.value = params.rmqUsername;
    rmqPassword.value = params.rmqPassword;
    rmqHost.value = params.rmqHost;
    gameId.value = params.gameId;
    isWebRTC.value = params.isWebRTC;
  }

  return {
    rmqIp,
    rmqUsername,
    rmqPassword,
    rmqHost,
    gameId,
    isWebRTC,
    connectState,
    getObservationSubs,
    getFireAndInfoSubs,
    getRmqHost,
    getRmqUsername,
    getRmqPassword,
    getRmqUrl,
    getGameId,
    setRmqValue,
    setConnectState
  };
});
